package com.foreveredu.propms.web.struts.action;

import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.beanutils.PropertyUtils;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;

import com.foreveredu.propms.domain.HouseSource;
import com.foreveredu.propms.service.IManagementService;
import com.foreveredu.propms.web.struts.actionform.HouseSourceActionForm;

public class HouseSourceAction extends AbstractBaseDispatchAction {

	private IManagementService managementService = null;

	public IManagementService getManagementService() {
		return managementService;
	}

	public void setManagementService(IManagementService managementService) {
		this.managementService = managementService;
	}


	// 跳转到收费项目列表页面
	public ActionForward list(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {

		List<HouseSource> HouseSourceList = managementService.findHouseSources();
		request.setAttribute("houseSourceList", HouseSourceList);

		return mapping.findForward("houseSourceList");
	}

	// 完成收费项目添加操作
	public ActionForward save(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		HouseSourceActionForm hsf = (HouseSourceActionForm) form;
		HouseSource HouseSource = new HouseSource();
		PropertyUtils.copyProperties(HouseSource, hsf);
		if (hsf.getName() == null || "".equals(hsf.getName())) {
			request.setAttribute("houseSource", HouseSource);
			request.setAttribute("error", "房产来源不能为空！");
			return mapping.findForward("toHouseSourceList");
		}
		managementService.saveHouseSource(HouseSource);
		return mapping.findForward("toHouseSourceList");
	}

	// 跳转到收费项目修改页面
	public ActionForward update(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		HouseSourceActionForm hsf = (HouseSourceActionForm) form;
		HouseSource HouseSource = managementService.findHouseSourceById(hsf.getId());
		request.setAttribute("houseSource", HouseSource);

		return mapping.findForward("houseSourceUpdate");
	}

	// 保存收费项目修改数据
	public ActionForward updateSave(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {

		HouseSourceActionForm hsf = (HouseSourceActionForm) form;
		HouseSource HouseSource = managementService.findHouseSourceById(hsf.getId());
		HouseSource.setName(hsf.getName());
		if(hsf.getName()==null || "".equals(hsf.getName()) ){
			request.setAttribute("houseSource", HouseSource);
			request.setAttribute("error","房产来源不能为空！");
			return mapping.findForward("houseSourceUpdate");
		}
		managementService.updateHouseSource(HouseSource);
		return mapping.findForward("update");
	}

	// 完成收费项目删除操作
	public ActionForward delete(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		HouseSourceActionForm hsf = (HouseSourceActionForm) form;
		managementService.deleteHouseSourceById(hsf.getId());
		return mapping.findForward("delete");
	}

}
